﻿using UnityEngine;
using System.Collections;

public class FriendListItem : LTEventListenerC {
    [HideInInspector]
    public AddSocialMenuItems manager;

	[HideInInspector]
    public int index;
    public UISprite friendUnitImage;
    public UILabel friendName;
    public UILabel friendLevel;
    public UILabel friendLastConnectionTime;
	
	public GameObject acceptButtonObject;
	public GameObject deleteButtonObject;

	public UILabel mailTypeLabel;

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    void DeleteFriend()
    {
		string[] param = new string[1];
		MessagePopup.window.ShowPopup(MESSAGEPOPUPTYPE.DeleteFriend, param, MessagePopupCallback);
    }

    void AcceptFriendRequest()
    {
        LoadingWindow.window.ShowShortLoading();
        RestAPI.singleton.AcceptFriendRequest(ReturnCallback, friendName.text);
    }

    void DenyFriendRequest()
    {
        LoadingWindow.window.ShowShortLoading();
        RestAPI.singleton.AcceptFriendRequest(ReturnCallback, friendName.text);
    }
	
	void AcceptMail()
	{
        LoadingWindow.window.ShowShortLoading();
		RestAPI.singleton.AcceptMail(ReturnCallback, MyInfo.Instance.MyMailboxList[index].id);
	}
	
	void DeleteMail()
	{
        LoadingWindow.window.ShowShortLoading();
		RestAPI.singleton.DeleteMail(ReturnCallback, MyInfo.Instance.MyMailboxList[index].id);
	}
	
	void MessagePopupCallback(MESSAGEPOPUPTYPE type, bool isYes)
	{
		LoadingWindow.window.ShowShortLoading();
		RestAPI.singleton.DeleteFriend(ReturnCallback, MyInfo.Instance.MyFriendList[index].friendName);
	}

    public override void OnEvent(int eventId)
    {
//        DestroyImmediate(CommonMsgBox.window.gameObject);
    }

    void ReturnCallback(Hashtable returnData)
    {
        LoadingWindow.window.HideLoading();
        switch ((RestAPIProtocol)returnData["protocol"])
        {
            case RestAPIProtocol.DELETEFRIEND:
                {
                    int tmpUserDBID = MyInfo.Instance.MyFriendRequestedList[index].userDBID;

                    MyInfo.Instance.MyFriendList.RemoveAt(index);
                    manager.SetItems(SocialMenuManager.singleton.currentSocialMenuType);

                    /* XMPP Presence Subscribe request */
                    {
                        CXmppManager xmppCli = GameObject.FindObjectOfType(typeof(CXmppManager)) as CXmppManager;
                        if (xmppCli != null)
                        {
                            xmppCli.RequestUnsubscribePresence(tmpUserDBID);
                        }
                    }
                }
                break;
            case RestAPIProtocol.ACCEPTFRIENDREQUEST:
                {
                    int tmpUserDBID = MyInfo.Instance.MyFriendRequestedList[index].userDBID;

                    MyInfo.Instance.MyFriendRequestedList.RemoveAt(index);
                    manager.SetItems(SocialMenuManager.singleton.currentSocialMenuType);
                    LoadingWindow.window.ShowShortLoading();

                    /* XMPP Presence Subscribe request */
                    {
                        CXmppManager xmppCli = GameObject.FindObjectOfType(typeof(CXmppManager)) as CXmppManager;
                        if(xmppCli != null)
                        {
                            xmppCli.RequestSubscribePresence(tmpUserDBID);
                        }
                    }
                    RestAPI.singleton.GetMyFriendList(ReturnCallback);
                }
                break;
            case RestAPIProtocol.DENYFRIENDREQUEST:
                MyInfo.Instance.MyFriendRequestedList.RemoveAt(index);
                manager.SetItems(SocialMenuManager.singleton.currentSocialMenuType);
                break;
            case RestAPIProtocol.GETMYFRIENDLIST:
                break;
			case RestAPIProtocol.ACCETPMAIL:
				int attachType = MyInfo.Instance.MyMailboxList[index].attachType;
				int attachMount = MyInfo.Instance.MyMailboxList[index].attachAmount;
				if( attachType == 1 )
					MyInfo.Instance.HPPotion += attachMount;
				else if( attachType == 2 )
					MyInfo.Instance.ResurrectPotion += attachMount;
				else if ( attachType == 3 )
					MyInfo.Instance.Money += attachMount;
				else if ( attachType == 4 )
					MyInfo.Instance.Diamond += attachMount;
			
				SocialMenuManager.singleton.topInfoBar.UpdateInfos();
                LoadingWindow.window.ShowShortLoading();
				RestAPI.singleton.DeleteMail(ReturnCallback, MyInfo.Instance.MyMailboxList[index].id);
                break;
			case RestAPIProtocol.DELETEMAIL:
				MyInfo.Instance.MyMailboxList.RemoveAt(index);
				manager.SetItems(SocialMenuManager.singleton.currentSocialMenuType);
                break;
        }
    }
}
